//
//  LLT_DataHelper_LXM.m
//  CoderLLT
//
//  Created by 刘晓萌 on 15/12/15.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_DataHelper_LXM.h"

#import "AFNetworking.h"


@implementation LLT_DataHelper_LXM

/**
 *  获取玩家基本信息
 *
 *  @param playerName 用户名
 *  @param serverName 服务器名
 */
-(void)getPlayerDetailByPlayerName:(NSString *)playerName andServerName:(NSString *)serverName{
    //设置URL地址
    NSString *url = PlayerDetail(serverName, playerName);
    url = [url stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)];
    
    //创建AF的管理对象类
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    //使用get方式来获取并解析数据
    [myManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dictionary = responseObject;
        LLT_PlayerModel *player = [[LLT_PlayerModel alloc]init];
        NSNumber *status = dictionary[@"status"];
        if ([status intValue] == 200) {
            [player setValuesForKeysWithDictionary:dictionary];
            player.serverName = serverName;
            player.playerName = playerName;
        }
        if(self.setPlayerInfo){
            self.setPlayerInfo(player);
        }
    }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               //NSLog(@"%@",error);
           }
     ];
}

/**
 *  获取玩家段位
 *
 *  @param player 玩家
 */
-(void)getPlayerRankClassByPlayerName:(NSString *)playerName andServerName:(NSString *)serverName{
    //设置URL地址
    NSString *url = PlayerRankClass(serverName, playerName);
    url = [url stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)];
    //创建AF的管理对象类
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    //使用get方式来获取并解析数据
    [myManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dictionary = responseObject;
        LLT_PlayerModel *player = [[LLT_PlayerModel alloc]init];
        NSNumber *status = dictionary[@"status"];
        if ([status intValue] == 200) {
            [player setValuesForKeysWithDictionary:dictionary];
        }
        if(self.setPlayerRankClass){
            self.setPlayerRankClass(player);
        }
    }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               //NSLog(@"%@",error);
           }
     ];
}

/**
 *  玩家战绩统计
 *
 *  @param playerName 用户名
 *  @param serverName 服务器名
 */
-(void)getPlayerRecordByPlayerName:(NSString *)playerName andServerName:(NSString *)serverName{
    //设置URL地址
    NSString *url = PlayerRecord(serverName, playerName);
    url = [url stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)];
    //创建AF的管理对象类
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    //使用get方式来获取并解析数据
    [myManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dictionary = responseObject;

        NSMutableArray *playerRecordArr = [NSMutableArray array];
        NSNumber *status = dictionary[@"status"];
        if ([status intValue] == 200) {
            NSDictionary *dicRecord = @{@"chaos":@"大乱斗",@"machine":@"人机对战",@"elrank":@"匹配赛",@"lineup":@"阵容模式"};
            
                for (NSString *key in [dicRecord allKeys]) {
                    LLT_PlayerRecordModel *playerRecord = [[LLT_PlayerRecordModel alloc]init];
                    [playerRecord setValuesForKeysWithDictionary:dictionary[key]];
                    playerRecord.fightType =dicRecord[key];
                    if (playerRecord.matchCount.length == 0) {
                        continue;
                    }
                    [playerRecordArr addObject:playerRecord];
                }
            
        }
        if(self.setPlayerRecord){
            self.setPlayerRecord(playerRecordArr);
        }
    }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               //NSLog(@"%@",error);
           }
     ];
}

/**
 *  玩家最近使用英雄
 *
 *  @param playerName 用户名
 *  @param serverName 服务器名
 */
-(void)getPlayerLastUsedByPlayerName:(NSString *)playerName andServerName:(NSString *)serverName{
    //设置URL地址
    NSString *url = PlayerLastUsed(serverName, playerName);
    url = [url stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)];
    //创建AF的管理对象类
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    //使用get方式来获取并解析数据
    [myManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dictionary = responseObject;
        NSMutableArray *playerLastArr = [NSMutableArray array];
        NSNumber *status = dictionary[@"status"];
        if ([status intValue] == 200) {
            NSArray *dicArr = dictionary[@"lastused"];
            for (NSDictionary *dic in dicArr) {
                LLT_PlayerLastUsedModel *playerLastUse = [[LLT_PlayerLastUsedModel alloc]init];
                [playerLastUse setValuesForKeysWithDictionary:dic];
                [playerLastArr addObject:playerLastUse];
            }
        }
        if(self.setPlayerLastUsed){
            self.setPlayerLastUsed(playerLastArr);
        }
    }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               //NSLog(@"%@",error);
           }
     ];
}

/**
 *  英雄使用数据统计
 *
 *  @param playerName 用户名
 *  @param serverName 服务器名
 */
-(void)getPlayerHeroRecordByPlayerName:(NSString *)playerName andServerName:(NSString *)serverName{
    //设置URL地址
    NSString *url = PlayerHeroRecord(serverName, playerName);
    url = [url stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)];
    //创建AF的管理对象类
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    //使用get方式来获取并解析数据
    [myManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dictionary = responseObject;
        
        NSMutableArray *playerHeroRecordArr = [NSMutableArray array];
        NSNumber *status = dictionary[@"status"];
        if ([status intValue] == 200) {
            NSArray *dicArr = dictionary[@"heroRecord"];
            for (NSDictionary *dic in dicArr) {
                LLT_PlayerHeroRecordModel *playerHeroRecord = [[LLT_PlayerHeroRecordModel alloc]init];
                [playerHeroRecord setValuesForKeysWithDictionary:dic];
                [playerHeroRecordArr addObject:playerHeroRecord];
            }
        }
        if(self.setPlayerHeroRecord){
            self.setPlayerHeroRecord(playerHeroRecordArr);
        }
    }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               //NSLog(@"%@",error);
           }
     ];
}

@end
